package com.ak.server.system.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ak.common.annotation.LogEndpoint;
import com.ak.common.controller.BaseController;
import com.ak.common.entity.AKResponse;
import com.ak.common.entity.QueryRequest;
import com.ak.common.entity.system.Dept;
import com.ak.common.utils.AKUtil;
import com.ak.server.system.service.IDeptService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

@Api(tags = "部门模块")
@Validated
@RestController
@RequestMapping("dept")
@RequiredArgsConstructor
public class DeptController extends BaseController {

    private final IDeptService deptService;

    @ApiOperation("查询部门集合")
    @GetMapping
    public AKResponse deptList(QueryRequest request, Dept dept) {
        return new AKResponse().data(this.deptService.findDepts(request, dept));
    }

    @ApiOperation("添加部门")
    @PostMapping
    @PreAuthorize("hasAuthority('dept:add')")
    @LogEndpoint(operation = "新增部门", exceptionMessage = "新增部门失败")
    public void addDept(@Valid Dept dept) {
    	// 租户管理员新增部门，所以部门的租户编码为当前登录用户的租户编码
    	dept.setCreateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        this.deptService.createDept(dept);
    }

    @ApiOperation("删除部门")
    @DeleteMapping("/{deptIds}")
    @PreAuthorize("hasAuthority('dept:delete')")
    @LogEndpoint(operation = "删除部门", exceptionMessage = "删除部门失败")
    public void deleteDepts(@NotBlank(message = "{required}") @PathVariable String deptIds) {
    	Integer[] ids = Arrays.stream(Arrays.stream(deptIds.split(StringPool.COMMA)).mapToInt(Integer::parseInt).toArray()).boxed().toArray(Integer[]::new);
        this.deptService.deleteDepts(ids);
    }

    @ApiOperation("修改部门信息")
    @PutMapping
    @PreAuthorize("hasAuthority('dept:update')")
    @LogEndpoint(operation = "修改部门", exceptionMessage = "修改部门失败")
    public void updateDept(@Valid Dept dept) {
    	dept.setUpdateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        this.deptService.updateDept(dept);
    }

    @ApiOperation("导出部门数据")
    @PostMapping("excel")
    @PreAuthorize("hasAuthority('dept:export')")
    @LogEndpoint(operation = "导出部门数据", exceptionMessage = "导出Excel失败")
    public void export(Dept dept, QueryRequest request, HttpServletResponse response) {
        List<Dept> depts = this.deptService.findDepts(dept, request);
        ExcelKit.$Export(Dept.class, response).downXlsx(depts, false);
    }
}
